package com.jie.langlangmall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.jie.langlangmall.common.constant.StatusConstant;
import com.jie.langlangmall.mapper.UmsResourceMapper;
import com.jie.langlangmall.model.UmsResource;
import com.jie.langlangmall.model.UmsResourceExample;
import com.jie.langlangmall.service.UmsAdminCacheService;
import com.jie.langlangmall.service.UmsResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/21 21:59
 * @Email 3217998214@qq.com
 */
@Service
public class UmsResourceServiceImpl implements UmsResourceService {

    @Autowired
    private UmsResourceMapper resourceMapper;
    @Autowired
    private UmsAdminCacheService adminCacheService;

    @Override
    public List<UmsResource> listAll() {
        return resourceMapper.selectByExample(new UmsResourceExample());
    }

    /**
     * 分页+参数查询
     *
     * @param categoryId
     * @param nameKeyword
     * @param urlKeyword
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public List<UmsResource> list(Long categoryId, String nameKeyword, String urlKeyword, Integer pageSize, Integer pageNum) {
        // 1. 开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        // 2. 构造查询参数
        UmsResourceExample example = new UmsResourceExample();
        UmsResourceExample.Criteria criteria = example.createCriteria();
        if (categoryId != null) {
            criteria.andCategoryIdEqualTo(categoryId);
        }
        if (StrUtil.isNotEmpty(nameKeyword)) {
            criteria.andNameLike("%" + nameKeyword + "%");
        }
        if (StrUtil.isNotEmpty(urlKeyword)) {
            criteria.andUrlLike('%' + urlKeyword + '%');
        }

        // 3. 从数据库查询
        return resourceMapper.selectByExample(example);
    }

    /**
     * 添加新的资源
     *
     * @param umsResource
     * @return
     */
    @Override
    public int create(UmsResource umsResource) {
        umsResource.setCreateTime(new Date());
        resourceMapper.insert(umsResource);
        // TODO: 添加新的资源需要更新缓存吗？
        return StatusConstant.SUCCESS;
    }

    /**
     * 更新资源
     *
     * @param id
     * @param umsResource
     * @return
     */
    @Override
    public int update(Long id, UmsResource umsResource) {
        umsResource.setId(id);
        int count = resourceMapper.updateByPrimaryKeySelective(umsResource);
        // 更新缓存
        adminCacheService.delResourceListByResource(id);
        return count > 0 ? StatusConstant.SUCCESS : count;
    }

    /**
     * 根据id获取资源详情
     *
     * @param id
     * @return
     */
    @Override
    public UmsResource getItem(Long id) {
        return resourceMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据id删除后台资源
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Long id) {
        // TODO: 删除资源后，其他关联表怎么办？
        int count = resourceMapper.deleteByPrimaryKey(id);
        adminCacheService.delResourceListByResource(id);
        return count > 0 ? StatusConstant.SUCCESS : count;
    }
}
